
package projetomsn.Client.UI.sounds;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.*;
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.SwingUtilities;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/**
 * Classe gerada para gerenciamento dos sons
 * @author Greg
 */
public class SoundManager implements Runnable {

    private String soundName;
    
    /**
     * Metodo construtor para o gerenciamento de som
     */
    public SoundManager(String s) {
        soundName = s;
    }
    
    /**
     * Executa o som selecionado
     */
    public static void play(String soundName) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        
        ExecutorService soundThread = Executors.newFixedThreadPool(10);
        soundThread.execute(new SoundManager(soundName));
    }

    @Override
    public void run() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                InputStream in = null;
                {
                    AudioStream as = null;
                    try {
                        in = new FileInputStream(new File(
                                "C://MyMessenger//Sounds//" + soundName + ".wav"));
                        as = new AudioStream(in);
                        AudioPlayer.player.start(as);
                    } catch (IOException ex) {
                        Logger.getLogger(SoundManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            }
        });
    }
}
